package mspbots.data.sync.config;

import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.RequiredArgsConstructor;
import org.springframework.amqp.core.*;
import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter;
import org.springframework.amqp.support.converter.MessageConverter;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.annotation.PostConstruct;

/**
 * com.mspbots.sync.config.RabbitConfigurer
 *
 * @author Alex bob(https://github.com/vnobo)
 * @date Created by 2019/7/29
 */
@Configuration
@RequiredArgsConstructor
public class RabbitConfiguration {

    public final static String DATA_CONNECTOR_TIMEDOCTOR = "data.connector.timedoctor";
    public final static String DATA_CONNECTOR_RIPPLING = "data.connector.rippling";
    public final static String DATA_CONNECTOR_TIMESHEET = "data.connector.timesheet";

    private final AmqpAdmin amqpAdmin;


    @Bean
    public MessageConverter rabbitMessageConverter(ObjectMapper objectMapper) {
        return new Jackson2JsonMessageConverter(objectMapper);
    }


    @Bean
    public void syncDataLogDeclareExchange() {
        // log API
        Exchange exchange = ExchangeBuilder.directExchange("data.exception.log").build();
        this.amqpAdmin.declareExchange(exchange);
        Queue queue = QueueBuilder.durable("data.exception.log").build();
        this.amqpAdmin.declareQueue(queue);
        this.amqpAdmin.declareBinding(BindingBuilder.bind(queue)
                .to(exchange).with("exception-log-key").noargs());
    }

    @PostConstruct
    public void timeDoctorDeclareExchange() {
        //companies API
        Exchange exchange = ExchangeBuilder.directExchange(DATA_CONNECTOR_TIMEDOCTOR).build();
        this.amqpAdmin.declareExchange(exchange);

        Queue queue = QueueBuilder.durable("timedoctor.companies.sync").build();
        this.amqpAdmin.declareQueue(queue);
        this.amqpAdmin.declareBinding(BindingBuilder.bind(queue)
                .to(exchange).with("timedoctor-companies-key").noargs());

        //users API
        queue = QueueBuilder.durable("timedoctor.users.sync").build();
        this.amqpAdmin.declareQueue(queue);
        this.amqpAdmin.declareBinding(BindingBuilder.bind(queue)
                .to(exchange).with("timedoctor-users-key").noargs());

        //projects API
        queue = QueueBuilder.durable("timedoctor.projects.sync").build();
        this.amqpAdmin.declareQueue(queue);
        this.amqpAdmin.declareBinding(BindingBuilder.bind(queue)
                .to(exchange).with("timedoctor-projects-key").noargs());

        //tasks API
        queue = QueueBuilder.durable("timedoctor.tasks.sync").build();
        this.amqpAdmin.declareQueue(queue);
        this.amqpAdmin.declareBinding(BindingBuilder.bind(queue)
                .to(exchange).with("timedoctor-tasks-key").noargs());

        //worklogs API
        queue = QueueBuilder.durable("timedoctor.worklogs.sync").build();
        this.amqpAdmin.declareQueue(queue);
        this.amqpAdmin.declareBinding(BindingBuilder.bind(queue)
                .to(exchange).with("timedoctor-worklogs-key").noargs());
    }

    @PostConstruct
    public void ripplingDeclareExchange() {
        //companies API
        Exchange exchange = ExchangeBuilder.directExchange(DATA_CONNECTOR_RIPPLING).build();
        this.amqpAdmin.declareExchange(exchange);

        Queue queue = QueueBuilder.durable("rippling.companies.sync").build();
        this.amqpAdmin.declareQueue(queue);
        this.amqpAdmin.declareBinding(BindingBuilder.bind(queue)
                .to(exchange).with("rippling-companies-key").noargs());

        //employees API
        queue = QueueBuilder.durable("rippling.employees.sync").build();
        this.amqpAdmin.declareQueue(queue);
        this.amqpAdmin.declareBinding(BindingBuilder.bind(queue)
                .to(exchange).with("rippling-employees-key").noargs());

        //departments API
        queue = QueueBuilder.durable("rippling.departments.sync").build();
        this.amqpAdmin.declareQueue(queue);
        this.amqpAdmin.declareBinding(BindingBuilder.bind(queue)
                .to(exchange).with("rippling-departments-key").noargs());

        //custom_fields API
        queue = QueueBuilder.durable("rippling.custom_fields.sync").build();
        this.amqpAdmin.declareQueue(queue);
        this.amqpAdmin.declareBinding(BindingBuilder.bind(queue)
                .to(exchange).with("rippling-custom_fields-key").noargs());

        //levels API
        queue = QueueBuilder.durable("rippling.levels.sync").build();
        this.amqpAdmin.declareQueue(queue);
        this.amqpAdmin.declareBinding(BindingBuilder.bind(queue)
                .to(exchange).with("rippling-levels-key").noargs());

        //pay_schedules API
        queue = QueueBuilder.durable("rippling.pay_schedules.sync").build();
        this.amqpAdmin.declareQueue(queue);
        this.amqpAdmin.declareBinding(BindingBuilder.bind(queue)
                .to(exchange).with("rippling-pay_schedules-key").noargs());

        //company_deductions API
        queue = QueueBuilder.durable("rippling.company_deductions.sync").build();
        this.amqpAdmin.declareQueue(queue);
        this.amqpAdmin.declareBinding(BindingBuilder.bind(queue)
                .to(exchange).with("rippling-company_deductions-key").noargs());

        //employee_deductions API
        queue = QueueBuilder.durable("rippling.employee_deductions.sync").build();
        this.amqpAdmin.declareQueue(queue);
        this.amqpAdmin.declareBinding(BindingBuilder.bind(queue)
                .to(exchange).with("rippling-employee_deductions-key").noargs());

        //leave_requests API
        queue = QueueBuilder.durable("rippling.leave_requests.sync").build();
        this.amqpAdmin.declareQueue(queue);
        this.amqpAdmin.declareBinding(BindingBuilder.bind(queue)
                .to(exchange).with("rippling-leave_requests-key").noargs());

        //company_activity API
        queue = QueueBuilder.durable("rippling.company_activity.sync").build();
        this.amqpAdmin.declareQueue(queue);
        this.amqpAdmin.declareBinding(BindingBuilder.bind(queue)
                .to(exchange).with("rippling-company_activity-key").noargs());
    }






}
